package com.samsung.sharepoint;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

/* This class creates an activity that lists down all the files and folders in the android file system. 
 * It extends ListActivity
 */

public class attach extends ListActivity {
	private File CurrentDir;
	private FileArrayAdapter adapter;
	long i;
	String ServerIP;

	/* OnCreate Function */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CurrentDir = new File("/sdcard/");
		fill(CurrentDir);
		Intent i = getIntent();
		ServerIP = i.getStringExtra("ServerIP");

	}

	/*
	 * This function is used to fill with the list of files and folders in the
	 * activity
	 */
	private void fill(File f) {
		File[] dirs = f.listFiles();
		this.setTitle("Current Dir: " + f.getName());
		List<option> dir = new ArrayList<option>();
		List<option> fls = new ArrayList<option>();
		try {
			for (File ff : dirs) {
				if (ff.isDirectory()) {
					dir.add(new option(ff.getName(), "Folder", ff
							.getAbsolutePath()));
				} else {
					i = ff.length();
					if (i < 1024) {
						fls.add(new option(ff.getName(), "File Size:"
								+ ff.length() + "Bytes", ff.getAbsolutePath()));
					} else if (i < (1024 * 1024)) {
						fls.add(new option(ff.getName(), "File Size:"
								+ ff.length() / 1024 + "KB", ff
								.getAbsolutePath()));
					} else {
						fls.add(new option(ff.getName(), "File Size:"
								+ ff.length() / 1024 / 1024 + "MB", ff
								.getAbsolutePath()));
					}
				}
			}
		} catch (Exception e) {

		}
		Collections.sort(dir);
		Collections.sort(fls);
		dir.addAll(fls);
		if (!f.getName().equalsIgnoreCase("sdcard"))
			dir.add(0,
					new option("..Parent Directory", "Parent Directory", f
							.getParent()));
		adapter = new FileArrayAdapter(attach.this, R.layout.attach, dir);
		this.setListAdapter(adapter);
	}

	/*
	 * Its a listener that calls onFileClick when a file is selected and the
	 * fill function when a directory is selected
	 */
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		option o = adapter.getItem(position);
		if (o.getdata().equalsIgnoreCase("folder")
				|| o.getdata().equalsIgnoreCase("parent directory")) {
			CurrentDir = new File(o.getpath());
			fill(CurrentDir);
		} else {
			onFileClick(o);
		}
	}

	/*
	 * adds the file name to an intent and sends it back to the previous
	 * activity to enable downloading
	 */
	private void onFileClick(option o) {
		// Toast.makeText(this,
		// "File Clicked: "+o.getname(),Toast.LENGTH_SHORT).show();
		Intent intent = new Intent();
		intent.putExtra("abc", o.getpath());
		intent.putExtra("ServerIP", ServerIP);
		setResult(1, intent);
		finish();
	}

	/*
	 * A listener to handle java.NullPointerException when the backbutton is
	 * pressed"
	 */
	@Override
	public void onBackPressed() {
		Intent intent = new Intent();
		setResult(0, intent);
		super.onBackPressed();
	}

}
